package com.lin.blog.web;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@Results({ @Result(name = "login", location = "${loginUrl}", type = "redirect") })
public class LoginAction extends CrudActionSupport {
	public static final String CURRENT_USER_NAME = "currentuser";
	private String username;
	private String password;
	private String loginUrl;

	@Override
	public String list() throws Exception {
		UserService userService = UserServiceFactory.getUserService();
		if (userService.getCurrentUser() != null) {
			ServletActionContext
					.getRequest()
					.getSession()
					.setAttribute(CURRENT_USER_NAME,
							userService.getCurrentUser());
			return SUCCESS;
		} else {
			String requestUrl = ServletActionContext.getRequest().getHeader(
					"Referer");
			if (requestUrl == null)
				requestUrl = ServletActionContext.getRequest().getRequestURI();
			loginUrl = userService.createLoginURL(requestUrl);
			return "login";
		}
	}

	@Override
	public String input() throws Exception {
		UserService userService = UserServiceFactory.getUserService();
		if (userService.isUserLoggedIn() && userService.isUserAdmin()) {

		}
		return null;
	}

	@Override
	public String save() throws Exception {
		return null;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	protected void afterAction() {

	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLoginUrl() {
		return loginUrl;
	}

	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}

}
